#coding=utf-8

from django.shortcuts import render,redirect
from .models import *
from hashlib import sha1
from django.http import JsonResponse
# Create your views here.
def register(request):
    return render(request,'df_user/register.html')

#注册
def registerHandle(request):
    post = request.POST
    uname = post.get('user_name')
    upwd = post.get('pwd')
    upw2 = post.get('cpwd')
    uemail = post.get('email')
    #判断两次密码
    if upw2 != upwd:
        return redirect('/user/register/')

    #加密
    s1 = sha1()
    s1.update(upwd.encode('utf-8'))
    upwd3=s1.hexdigest()

    #创建对象
    user = UserInfo()
    user.uname = uname
    user.upwd = upwd3
    user.uemail = uemail
    user.save()
    return redirect('/user/login/')

def register_Exist(request):
    uname = request.GET['uname']
    count=UserInfo.objects.filter(uname=uname).count()
    print('数量：'+count)
    return JsonResponse({'count':count})